<template>
  <div class="sub-category">
    <div class="container">
      <!--  面包屑-->
      <SubBread/>
      <!--  筛选区-->
      <SubFilter @filterChange="filterChange"/>
      <!--  商品面板（排序组件+列表）-->
      <div class="goods-list">
        <!-- 排序选项组件 -->
        <SubSort @sortChange="sortChange"/>
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodList" :key="goods.id">
            <GoodsItem :goods="goods"/>
          </li>
        </ul>
        <!--  无限加载组件-->
        <XtxInfiniteLoading @infinite="getData" :loading="loading" :finished="finished"/>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from '@/views/category/components/sub-bread'
import SubFilter from '@/views/category/components/sub-filter'
import { ref, watch } from 'vue'
import SubSort from '@/views/category/components/sub-sort'
import GoodsItem from '@/views/category/components/goods-item'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading'
import { useRoute } from 'vue-router'
import { findSubCategoryGoods } from '@/api/category'

export default {
  name: 'subCategory',
  components: {
    XtxInfiniteLoading,
    GoodsItem,
    SubSort,
    SubFilter,
    SubBread
  },
  setup () {
    const isAllChecked = ref(false)

    const route = useRoute()
    // 加载中
    const loading = ref(false)
    // 是否加载完毕
    const finished = ref(false)
    // 商品列表数据
    const goodList = ref([])
    // 请求参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    const getData = () => {
      loading.value = true
      //  设置二级分类的ID
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(({ result }) => {
        //  获取数据成功
        if (result.items && result.items.length) {
          goodList.value.push(...result.items)
          // 把page改为下一个页码
          reqParams.page++
        } else {
          // 没有数据代表加载完成
          finished.value = true
        }
        loading.value = false
      })
    }
    // 在更改了二级分类的时候需要重新加载数
    watch(() => route.params.id, (newVal) => {
      if (newVal && '/category/sub/' + newVal === route.path) {
        finished.value = false
        // 导致列表为空的，加载更多组件顶上来，进入可视区，加载数据
        goodList.value = []
        reqParams.page = 1
        reqParams.pageSize = 20
      }
    }, { immediate: true })
    // （1）.更改排序组件的筛选属性，重新请求
    const sortChange = (sortParams) => {
      finished.value = false
      // 合并请求参数，需要保留之前的参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodList.value = []
    }
    // （2）.更改筛选组件的筛选属性，重新请求
    const filterChange = (filterParams) => {
      finished.value = false
      // 合并请求参数，需要保留之前的参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodList.value = []
    }
    return {
      isAllChecked,
      getData,
      loading,
      finished,
      goodList,
      sortChange,
      filterChange
    }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;

    li {
      margin-right: 20px;
      margin-bottom: 20px;

      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
